package com.yb.oldlearning;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 任务返回值测试
 * @author Administrator
 */
public class CallableDemo {
	/**
	 * submit()方法返回Future对象
	 * isDone()用来查询Future是否已经完成
	 * @param args
	 */
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newCachedThreadPool();
		List<Future<String>> resultList = new ArrayList<Future<String>>();
		for (int i = 0; i < 5; i++) {
			Future<String> future = executorService.submit(new TaskWithResult(i));
			System.out.println(future.isDone());
			resultList.add(future);
			System.out.println(future.isDone());
		}
		for (Future<String> future : resultList) {
			try {
				System.out.println(future.get());
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}finally {
				executorService.shutdown();
			}
		}
	}
}
